package com.mallcloud.mall.app.api.constant;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.http.Method;
import com.mallcloud.mall.app.api.entity.vo.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用api常量
 */
public interface ApiConstants {

	/**
	 * https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/
	 * @param <T>
	 */
	@Getter
	@Builder
	@AllArgsConstructor
	class WxApi<T> extends BaseApi<T> {

		public static WxApi<WxApiPhoneNumberVO> getUserPhoneNumber = WxApi.<WxApiPhoneNumberVO>builder()
				.url("https://api.weixin.qq.com/wxa/business/getuserphonenumber")
				.name("getUserPhoneNumber")
				.description("获取手机号")
				.method(Method.POST)
				.responseClass(WxApiPhoneNumberVO.class)
				.build();

		public static WxApi<WxApiAccessTokenVO> getAccessToken = WxApi.<WxApiAccessTokenVO>builder()
				.url("https://api.weixin.qq.com/cgi-bin/token")
				.name("getAccessToken")
				.description("获取接口调用凭据")
				.method(Method.GET)
				.responseClass(WxApiAccessTokenVO.class)
				.build();

		public static WxApi<WxApiJsCodeToSessionVO> jsCode2session  = WxApi.<WxApiJsCodeToSessionVO>builder()
				.url("https://api.weixin.qq.com/sns/jscode2session")
				.name("jsCode2session")
				.description("小程序登录")
				.method(Method.GET)
				.responseClass(WxApiJsCodeToSessionVO.class)
				.build();

		/**
		 * 渠道
		 */
		public final String channel = "wx";

		/**
		 * 地址
		 */
		private final String url;

		/**
		 * 名称
		 */
		private final String name;

		/**
		 * 请求方式
		 */
		private final Method method;

		/**
		 * 描述
		 */
		private final String description;

		/**
		 * 响应类型
		 */
		private final Class<T> responseClass;
	}

	@Getter
	@Builder
	@AllArgsConstructor
	class WxPayApi<T> extends BaseApi<T> {

		public static WxPayApi<String> UNIFIED_ORDER_V2 = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/pay/unifiedorder")
				.name("UNIFIED_ORDER_V2")
				.description("统一下单")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> UNIFIED_ORDER_V3 = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/pay/transactions")
				.name("UNIFIED_ORDER_V3")
				.description("统一下单")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> ORDER_QUERY_BY_NO = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/")
				.name("ORDER_QUERY_BY_NO")
				.description("查询订单")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> CLOSE_ORDER_BY_NO = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/%s/close")
				.name("CLOSE_ORDER_BY_NO")
				.description("关闭订单")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> DOMESTIC_REFUNDS = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds")
				.name("DOMESTIC_REFUNDS")
				.description("申请退款")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> DOMESTIC_REFUNDS_QUERY = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/")
				.name("DOMESTIC_REFUNDS_QUERY")
				.description("查询单笔退款")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> TRADE_BILLS = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/bill/tradebill")
				.name("TRADE_BILLS")
				.description("申请交易账单")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static WxPayApi<String> FUND_FLOW_BILLS = WxPayApi.<String>builder()
				.url("https://api.mch.weixin.qq.com/v3/bill/fundflowbill")
				.name("FUND_FLOW_BILLS")
				.description("申请资金账单")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		/**
		 * 渠道
		 */
		public final String channel = "wx_pay";

		/**
		 * 地址
		 */
		private final String url;

		/**
		 * 名称
		 */
		private final String name;

		/**
		 * 请求方式
		 */
		private final Method method;

		/**
		 * 描述
		 */
		private final String description;

		/**
		 * 响应类型
		 */
		private final Class<T> responseClass;
	}

	@Getter
	@Builder
	@AllArgsConstructor
	class XglApi<T> extends BaseApi<T> {

		public static MmApi<XglParkingResponseVO> INORDER = MmApi.<XglParkingResponseVO>builder()
				.url("https://park.zhenbokeji.com/openapi/orderInfo/inOrder")
				.name("INORDER")
				.description("停车账单")
				.method(Method.GET)
				.responseClass(XglParkingResponseVO.class)
				.build();

		public static MmApi<XglParkingResponseVO> PRICE = MmApi.<XglParkingResponseVO>builder()
				.url("https://park.zhenbokeji.com/openapi/orderInfo/price")
				.name("PRICE")
				.description("停车账单")
				.method(Method.GET)
				.responseClass(XglParkingResponseVO.class)
				.build();

		public static MmApi<XglParkingResponseVO> DERATE = MmApi.<XglParkingResponseVO>builder()
				.url("https://park.zhenbokeji.com/openapi/orderInfo/derate")
				.name("DERATE")
				.description("减免")
				.method(Method.POST)
				.responseClass(XglParkingResponseVO.class)
				.build();

		public static MmApi<XglQRCodePaymentInfoVO> QRCODE_PAYMENT_INFO = MmApi.<XglQRCodePaymentInfoVO>builder()
				.url("/QrNative")
				.name("QRCODE_PAYMENT_INFO")
				.description("获取立牌扫码支付信息")
				.method(Method.POST)
				.responseClass(XglQRCodePaymentInfoVO.class)
				.build();

		/**
		 * 渠道
		 */
		public final String channel = "xgl";

		/**
		 * 地址
		 */
		private final String url;

		/**
		 * 名称
		 */
		private final String name;

		/**
		 * 请求方式
		 */
		private final Method method;

		/**
		 * 描述
		 */
		private final String description;

		/**
		 * 响应类型
		 */
		private final Class<T> responseClass;
	}

	/**
	 * 美美停车场
	 *
	 * @param <T>
	 */
	@Getter
	@Builder
	@AllArgsConstructor
	class MmApi<T> extends BaseApi<T> {

		public static MmApi<MmParkingResponseVO> QUERY_CAR_INFO = MmApi.<MmParkingResponseVO>builder()
				.url("http://kp-open.keytop.cn/unite-api/api/wec/QueryCarInfo")
				.name("QUERY_CAR_INFO")
				.serviceCode("queryCarInfo")
				.description("查询在场车辆")
				.method(Method.POST)
				.responseClass(MmParkingResponseVO.class)
				.build();

		public static MmApi<MmParkingResponseVO> GET_PARKING_PAYMENT_INFO = MmApi.<MmParkingResponseVO>builder()
				.url("http://kp-open.keytop.cn/unite-api/api/wec/GetParkingPaymentInfo")
				.name("GET_PARKING_PAYMENT_INFO")
				.serviceCode("getParkingPaymentInfo")
				.description("停车账单")
				.method(Method.POST)
				.responseClass(MmParkingResponseVO.class)
				.build();

		public static MmApi<MmParkingResponseVO> PAY_PARKING_FEE = MmApi.<MmParkingResponseVO>builder()
				.url("http://kp-open.keytop.cn/unite-api/api/wec/PayParkingFee")
				.name("PAY_PARKING_FEE")
				.serviceCode("payParkingFee")
				.description("停车费支付")
				.method(Method.GET)
				.responseClass(MmParkingResponseVO.class)
				.build();

		public static MmApi<MmParkingResponseVO> GET_BACK_FEE_ORDER_INFO = MmApi.<MmParkingResponseVO>builder()
				.url("http://kp-open.keytop.cn/unite-api/config/platform/GetBackFeeOrderInfo")
				.name("GET_BACK_FEE_ORDER_INFO")
				.serviceCode("getBackFeeOrderInfo")
				.description("账单查询")
				.method(Method.POST)
				.responseClass(MmParkingResponseVO.class)
				.build();

		public static MmApi<String> POST_PAY_FEE_INFO = MmApi.<String>builder()
				.url("http://kp-open.keytop.cn/unite-api/PostPayFeeInfo")
				.name("POST_PAY_FEE_INFO")
				.serviceCode("postPayFeeInfo")
				.description("缴费记录上报")
				.method(Method.POST)
				.responseClass(String.class)
				.build();

		public static MmApi<MmParkingResponseVO> GET_PARKING_PAYMENT_LIST = MmApi.<MmParkingResponseVO>builder()
				.url("http://kp-open.keytop.cn/unite-api/api/wec/GetParkingPaymentList")
				.name("GET_PARKING_PAYMENT_LIST")
				.serviceCode("paymentRecordList")
				.description("查询缴费记录")
				.method(Method.POST)
				.responseClass(MmParkingResponseVO.class)
				.build();

		public static MmApi<MmParkingResponseVO> GET_ORDER_STATUS = MmApi.<MmParkingResponseVO>builder()
				.url("http://kp-open.keytop.cn/unite-api/api/wec/GetOrderStatus")
				.name("GET_ORDER_STATUS")
				.serviceCode("getOrderStatus")
				.description("查询订单状态")
				.method(Method.POST)
				.responseClass(MmParkingResponseVO.class)
				.build();

		/**
		 * 渠道
		 */
		public final String channel = "mm";

		/**
		 * 地址
		 */
		private final String url;

		/**
		 * 名称
		 */
		private final String name;
		/**
		 * 服务code
		 */
		private final String serviceCode;
		/**
		 * 请求方式
		 */
		private final Method method;

		/**
		 * 描述
		 */
		private final String description;

		/**
		 * 响应类型
		 */
		private final Class<T> responseClass;
		/**
		 * 默认请求头
		 */
		public final static Map header = MapBuilder.create(new HashMap<String, String>())
				.put("version", "1.0.0")
				.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
				.build();
	}

	@Getter
	abstract class BaseApi<T> {

		/**
		 * 第三方枚举
		 *
		 * @return 第三方枚举
		 */
		public abstract String getChannel();

		/**
		 * api 名称
		 *
		 * @return api 名称
		 */
		public abstract String getName();

		/**
		 * 请求方式
		 *
		 * @return api 名称
		 */
		public abstract Method getMethod();

		/**
		 * api 返回
		 *
		 * @return api 名称
		 */
		public abstract Class<T> getResponseClass();

		/**
		 * api url
		 *
		 * @return api url
		 */
		public abstract String getUrl();

		/**
		 * api描述
		 *
		 * @return api描述
		 */
		public abstract String getDescription();
	}
}
